package com.woniuxy.microgoldservice.controller;

import com.alibaba.fastjson.JSONObject;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.microgoldservice.bean.GoldOrderDTO;
import com.woniuxy.microgoldservice.service.IGoldPurchaseService;
import com.woniuxy.microgoldservice.utils.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author hongbin
 * @date 2021-04-13 18:35
 * Description：
 */
@RefreshScope
@Api(value = "购买黄金接口")
@RestController
@RequestMapping(value = "/goldOrder", produces = "application/json;charset=utf-8")
@Slf4j
public class GoldOrderController {

    private final ResultBean MSG = ResultBean.builder().code(Integer.parseInt(CodeEnum.PARAMETER_ERROR.getCode())).msg(CodeEnum.PARAMETER_ERROR.getMessage()).build();
    @Resource
    private IGoldPurchaseService goldPurchaseServiceImpl;

    @ApiOperation(value = "黄金总资产", httpMethod = "GET")
    @GetMapping("/asset")
    public ResultBean asset(String userName){
        if(userName==null || userName.isEmpty()){
            return MSG;
        }
        return goldPurchaseServiceImpl.getAsset(userName);
    }

    @GetMapping("/holdNumber")
    public ResultBean holdGrammage(String userName){
        if (userName == null || userName.isEmpty()) {
            return MSG;
        }
        return goldPurchaseServiceImpl.getHoldGrammage(userName);
    }

    @ApiOperation(value = "黄金收益", httpMethod = "GET")
    @GetMapping("/earnings")
    public ResultBean getEarnings(String userName) {
        if (userName == null || userName.isEmpty()) {
            return MSG;
        }
        return goldPurchaseServiceImpl.getPurchase(userName);
    }

    @ApiOperation(value = "黄金购买接口", httpMethod = "POST")
    @PostMapping("/buy")
    public ResultBean purchaseGold(@RequestBody GoldOrderDTO goldOrderDTO) {
        if (goldOrderDTO.getProId() == null
                || goldOrderDTO.getProId() <= 0

                || goldOrderDTO.getUsername() == null
                || goldOrderDTO.getUsername().isEmpty()

                || goldOrderDTO.getMoney() == null
                || goldOrderDTO.getMoney() <= 0

                || goldOrderDTO.getPwd() == null
                || goldOrderDTO.getPwd().isEmpty()

                || goldOrderDTO.getPayType() == null
                || goldOrderDTO.getPayType().isEmpty()

        ) {
            return MSG;
        }

        return goldPurchaseServiceImpl.buyGold(goldOrderDTO);

    }


    @ApiOperation(value = "黄金赎回接口", httpMethod = "POST")
    @PostMapping("/redemption")
    public ResultBean redemptionGold(@RequestBody GoldOrderDTO goldOrderDTO) {


        if (goldOrderDTO.getProId() == null
                || goldOrderDTO.getProId() <= 0

                || goldOrderDTO.getGrammage() == null
                || goldOrderDTO.getGrammage() <= 0

                || goldOrderDTO.getUsername() == null
                || goldOrderDTO.getUsername().isEmpty()

                || goldOrderDTO.getPwd() == null
                || goldOrderDTO.getPwd().isEmpty()

                || goldOrderDTO.getPayType() == null
                || goldOrderDTO.getPayType().isEmpty()) {
            return MSG;
        }
        return goldPurchaseServiceImpl.redemptionGold(goldOrderDTO);

    }


}
